import { getNews, login } from './request.js';
import utils from './utils.js';

// 立刻执行函数的作用
// 1. 闭包，外部人员无法修改数据
// 2. 不需要外部再定义一个函数配合 async await
(async function () {
    let res = await login()
    let token = res.data.token
    utils.myStorage.set("token", token, 60)

    let news = await getNews()
    let newsArr = news.data.rows
    Rendering(newsArr)
})()

// 渲染
function Rendering(arr) {
    let newBox = document.querySelector("#new");
    let str = "";
    arr.forEach(item => {
        str += `
            <div class="item" >
                <div class="imgBox"><img src="${item.cover}" alt=""></div>
                <div class="textBox">${item.title}</div>
                <div class="timeBox">发布时间：${item.sketch}</div>
            </div>
        `
    });
    newBox.innerHTML = str;
}
